package cn.wq3stone.dao.impl;

import cn.wq3stone.dao.UserDao;
import cn.wq3stone.pojo.User;
import cn.wq3stone.util.DBUtil;

import java.sql.*;
import java.util.List;

/**
 * @ClassName UserServiceImpl.java
 * @Description
 * @Author cn.wq3stone
 * @Date 2023/4/24 21:09
 */
public class UserDaoImpl implements UserDao {
    public int addUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        int flag = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "insert into t_user (username,password) values (?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2, user.getPassword());
            flag = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,null);
        }
        return flag;
    }

    public int deleteUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        int flag = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "delete from t_user where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user.getId());
            flag = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,null);
        }
        return flag;
    }

    public User queryById(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,username,password from t_user where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                String username = rs.getString("username");
                String password = rs.getString("password");
                user = new User(id, username, password);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        return user;
    }

    public User queryByUame(String uname) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,username,password from t_user where username = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,uname);
            rs = ps.executeQuery();
            if(rs.next()){
                Integer id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                user = new User(id, username, password);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        return user;
    }

    public List<User> queryAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> userList = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,username,password from t_user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Integer id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                userList.add(new User(id,username,password));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        return userList;
    }

    public int updatePwd(String pwd, int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        int flag = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "update t_user set password=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,pwd);
            ps.setInt(2,id);
            flag = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
}
